//index.js
let network = require('../../utils/requestManager.js')
//获取应用实例
const app = getApp()

Page({
  data: {
    switch_true:true,
    recharge_money:0,
    hb_money:0,
    discount_num:0,
    deal_money:null,
    image_url: '',
    title: '',
    owner: false,
    win_height: '',
    last_money: null,
    record_count: '',

    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },

  //请求数据
  request_data:function(){
    let that = this;
    console.log("000")
    network.getRequest('backend/shop/v1/shop/api/seller/shop_info/money/obtain',{},function(res){
      console.log(res)      
      wx.setStorageSync('qrcode_money', res.detail.show_url);
      getApp().globalData.offline_discount = res.detail.offline_discount / 10;
      that.setData({
        recharge_money: res.detail.balance / 100,
        hb_money: res.detail.bei / 100,
        discount_num: res.detail.offline_discount / 10,
        image_url: res.detail.image_url,
        title: res.detail.title,
        record_count: res.detail.record_count,
      })
    },function(err){
      console.log(err)
    })
  },

  onLoad: function (option) {
    let that  = this;
    console.log(option)
    that.request_data()
    that.setData({
      win_height: app.globalData.winHeight
    })
  },

  onShow:function(){
    let that = this;
    let app = getApp();
    that.setData({
      discount_num: app.globalData.offline_discount,
      owner: app.globalData.owner,
    })

    if (that.data.switch_true){
      that.run_socket(that);
    }
  },

  run_socket: function(that){
    // 创建socket连接
    wx.closeSocket();
    wx.connectSocket({
      url: 'wss://shop.hbsse.com/ws/websockettest/?token=' + app.globalData.token
    })

    wx.onSocketOpen(function (res) {
      console.log('WebSocket连接已打开！')
      wx.sendSocketMessage({
        data: '{"shop":"1000056", "msg_type": 1201}',
      })
    })

    wx.onSocketMessage(function (res) {
      console.log(res);
      let data = JSON.parse(res.data);
      console.log(data);
      if(data.message.media_url){
        network.playVoice(data.message.media_url);
      }
      if(data.message.money){
        that.setData({
          last_money: data.message.money/100,
        })
      }
      if (data.message.bei) {
        that.setData({
          hb_money: data.message.bei/100,
          recharge_money: data.message.balance/100,
          record_count: data.message.record_count,
        })
      }
    })

    wx.onSocketClose(function (res) {
      console.log('WebSocket连接已关闭！')
    })

  },
  /**
   * 点击切换语音提醒
   * **/
  change_switch:function(){
    let that = this;
    let is_switch = that.data.switch_true;
    if(is_switch == true){
      that.setData({
        switch_true: false
      });
      wx.closeSocket();
    }else{
      that.setData({
        switch_true: true
      });
      that.run_socket(that);
    }
  },

  // 退出
  logout: function(){
    let app = getApp();
    app.globalData.code = '';
    app.globalData.owner = false;
    app.globalData.openid = '';
    app.globalData.token = '';
    app.globalData.money_qrcode = '';
    app.globalData.offline_discount = 0;
    wx.closeSocket();
    wx.reLaunch({
      url: '/pages/login/login_in',
    })
  },

  /**
   * 收款码
   * **/
  to_qrcode:function(){
    wx.navigateTo({
      url: 'qrcode/qrcode',
    })
  },
  /**充值**/
  to_recharge:function(){
    wx.navigateTo({
      url: 'recharge/recharge',
    })
  },

  /**提现**/
  to_withdraw:function(){
    wx.navigateTo({
      url: 'withdraw/withdraw?all_money=' + this.data.hb_money,
    })
  },

  /**交易记录**/
  to_deal_record:function(){
    wx.navigateTo({
      url: 'deal_record/deal_record',
    })
  },

  /**
   * 支付折扣优惠
   * **/
  to_pay_discount:function(){
    wx.navigateTo({
      url: 'pay_discount/pay_discount',
    })
  },
  /**提现记录**/
  to_withdraw_record:function(){
    wx.navigateTo({
      url: 'withdraw_record/withdraw_record',
    })
  },

  /**修改支付密码**/
  to_set_password:function(){
    wx.navigateTo({
      url: 'set_password/set_password',
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
